package cn.cdu.doctor.controller;

import cn.cdu.basics.baseVo.PageVo;
import cn.cdu.basics.baseVo.Result;
import cn.cdu.basics.utils.PageUtil;
import cn.cdu.basics.utils.ResultUtil;
import cn.cdu.data.utils.NullUtils;
import cn.cdu.doctor.entity.HospitalSubject;
import cn.cdu.doctor.service.IHospitalSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@RestController
@Tag(name = "科室管理")
@RequestMapping("/zwz/hospitalSubject")
@Transactional
public class HospitalSubjectController {

    @Autowired
    private IHospitalSubjectService iHospitalSubjectService;

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @Operation(summary = "查询所有科室")
    public Result<List<HospitalSubject>> getAll() {
        return new ResultUtil<List<HospitalSubject>>().setData(iHospitalSubjectService.list());
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @Operation(summary = "查询科室")
    public Result<IPage<HospitalSubject>> getByPage(@ModelAttribute HospitalSubject subject, @ModelAttribute PageVo page) {
        QueryWrapper<HospitalSubject> qw = new QueryWrapper<>();
        if (!NullUtils.isNull(subject.getSubName())) {
            qw.like("sub_name", subject.getSubName());
        }
        if (!NullUtils.isNull(subject.getSubCode())) {
            qw.like("sub_code", subject.getSubCode());
        }
        return new ResultUtil<IPage<HospitalSubject>>().setData(iHospitalSubjectService.page(PageUtil.initMpPage(page), qw));
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @Operation(summary = "新增修改科室")
    public Result<HospitalSubject> saveOrUpdate(@RequestBody HospitalSubject hospitalSubject) {
        if (iHospitalSubjectService.saveOrUpdate(hospitalSubject)) {
            return new ResultUtil<HospitalSubject>().setData(hospitalSubject);
        }
        return ResultUtil.error();
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @Operation(summary = "删除科室")
    public Result<Object> delAllByIds(@RequestBody String[] ids) {
        for (String id : ids) {
            iHospitalSubjectService.removeById(id);
        }
        return ResultUtil.success();
    }
}
